import { Component } from '@tarojs/taro'

let startX: any
let startY: any
let endX: any
let endY: any
let moveFlag = false

export default class FyComponent<P = {}, S = {}> extends Component<P, S> {
  state: any
  props: any

  static options = {
    addGlobalClass: true
  }

  constructor() {
    super(...arguments)
    this.state = {
    }
  }

  //  监听开始触摸事件
  onTouchStart (e) {
    startX = e.touches[0].pageX
    startY = e.touches[0].pageY
    moveFlag = true
  }

  // 触摸移动事件
  onTouchMove () {
    const e = arguments[arguments.length - 1]
    endX = e.touches[0].pageX // 获取触摸时的原点
    endY = e.touches[0].pageY // 获取触摸时的原点
    if (moveFlag) {
      const num = Math.abs(endY - startY) < 50
      if (num && endX - startX > 50) {
        this.onTouchRight(...arguments)
        moveFlag = false
      }
      if (num && startX - endX > 50) {
        this.onTouchLeft(arguments)
        moveFlag = false
      }
    }
  }

  //  触摸移动结束
  onTouchEnd () {
    moveFlag = true
  }

  onTouchLeft() {
    console.log('onTouchLeft')
  }

  onTouchRight() {
    console.log('onTouchRight')
  }
}
